<!--
 * @Author: H.
 * @Date: 2021-12-06 18:12:03
 * @LastEditTime: 2021-12-14 13:48:42
 * @Description:
-->
<template>
  <div class="role">
    <page-search
      :searchFormConfig="searchFormConfig"
      @queryBtnClick="handleQueryClick"
      @resetBtnClick="handleResetClick"
    ></page-search>
    <page-content
      :contentTableConfig="contentTableConfig"
      pageName="role"
      ref="pageContentRef"
      @newBtnClick="handleNewData"
      @editBtnClick="handleEditData"
    ></page-content>
    <page-modal
      :pageModalConfig="pageModalConfig"
      pageName="role"
      ref="pageModalRef"
      :otherInfo="otherInfo"
      :pageModalTitle="pageModalTitle"
      :defaultInfo="defaultInfo"
    >
      <div class="menu-tree">
        <el-tree
          ref="elTreeRef"
          :data="menus"
          show-checkbox
          node-key="id"
          @check="hanldeCheckChange"
          highlight-current
          :props="{ children: 'children', label: 'name' }"
        />
      </div>
    </page-modal>
  </div>
</template>

<script lang="ts">
import { defineComponent, computed, ref, nextTick } from 'vue'

import PageSearch from '@/components/page-search'
import PageContent from '@/components/page-content'
import pageModal from '@/components/page-modal'

import { contentTableConfig } from './config/content.config'
import { searchFormConfig } from './config/search.config'
import { pageModalConfig } from './config/modal.config'

import { usePageModal } from '@/hooks/usePageModal'
import { usePageSearch } from '@/hooks/usePageSearch'

import { useStore } from '@/store'
import { getMenuLeafKeys } from '@/utils/map-menus'

import { ElTree } from 'element-plus'
export default defineComponent({
  name: 'role',
  setup() {
    const elTreeRef = ref<InstanceType<typeof ElTree>>()
    const newCallback = () => {
      pageModalTitle.value = '新增角色'
    }
    const editCallback = (item: any) => {
      const leafKeys = getMenuLeafKeys(item.menuList)
      pageModalTitle.value = '编辑角色'

      nextTick(() => {
        elTreeRef.value?.setCheckedKeys(leafKeys, false)
      })
    }
    const [pageModalRef, defaultInfo, handleNewData, handleEditData] =
      usePageModal(newCallback, editCallback)
    const pageModalTitle = ref('新增角色')

    const [pageContentRef, handleResetClick, handleQueryClick] = usePageSearch()

    const store = useStore()
    const menus = computed(() => store.state.entireMenu)

    const otherInfo = ref({})
    const hanldeCheckChange = (data1: any, data2: any) => {
      const checkedKeys = data2.checkedKeys
      const halfCheckedKeys = data2.halfCheckedKeys
      const menuList = [...checkedKeys, ...halfCheckedKeys]
      otherInfo.value = { menuList }
    }
    return {
      contentTableConfig,
      searchFormConfig,
      pageModalConfig,
      pageModalRef,
      defaultInfo,
      handleNewData,
      handleEditData,
      pageContentRef,
      handleResetClick,
      handleQueryClick,
      menus,
      otherInfo,
      hanldeCheckChange,
      elTreeRef,
      pageModalTitle
    }
  },
  components: {
    PageContent,
    PageSearch,
    pageModal
  }
})
</script>

<style scoped>
.menu-tree {
  margin-left: 25px;
}
</style>
